package com.gitee.wsl.func.transform.sample

import com.gitee.wsl.func.factory.Factory
import com.gitee.wsl.func.factory.Factory2
import com.gitee.wsl.func.transform.Transformer

/**
 * Transformer implementation that calls a Factory and returns the result.
 *
 * @param <T> the type of the input to the function.
 * @param <R> the type of the result of the function.
 * @since 3.0
</R></T> */
class FactoryTransformer<T, R>(private val factory: Factory2<T, R>) : Transformer<T, R>{

    /**
     * Transforms the input by ignoring the input and returning the result of
     * calling the decorated factory.
     *
     * @param input  the input object to transform
     * @return the transformed result
     */
     override fun transform(input: T): R {
        return factory.create(input)
    }

    companion object {

        /**
         * Factory method that performs validation.
         *
         * @param <I>  the input type
         * @param <O>  the output type
         * @param factory  the factory to call, not null
         * @return the `factory` transformer
         * @throws NullPointerException if the factory is null
        </O></I> */
        fun <I, O> factoryTransformer(factory: Factory2<I, O>): Transformer<I, O> {
            return FactoryTransformer(factory)
        }

        fun <I, O> factoryTransformer(factory: Factory<O>): Transformer<I, O> {
            return FactoryTransformer<I, O>{ factory.create() }
        }
    }
}
